﻿using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace PlcTest {
    public  class param {
        public static ModbusIpMaster nModbustcpnet = null;
        public static TcpClient tcpClient = new TcpClient();
        public static bool PLC1_State = false;

        public static string TcpModbusConnect(string ip, int port) {
            try {
                if (!param.tcpClient.Connected) {
                    param.tcpClient = new System.Net.Sockets.TcpClient();
                    param.tcpClient.BeginConnect(ip, port, null, null);
                    param.nModbustcpnet = ModbusIpMaster.CreateIp(param.tcpClient);
                }
                var tempresult = param.tcpClient.Connected;
                param.PLC1_State = tempresult;
                return "OK";
            } catch (Exception ep) {
                return ("PLC1连接失败！" + ep.Message);
            }
        }

        #region 获取字符
        public static string GetString(ushort[] src, int start, int len) {
            ushort[] temp = new ushort[len];
            for (int i = 0; i < len; i++) {
                temp[i] = src[i + start];

            }
            byte[] bytestemp = Ushorts2Bytes(temp);
            string res = Encoding.Default.GetString(bytestemp).Trim(new char[] { '\0' }).Replace("\02", "").Replace("\09", "");
            return res;
        }
        public static ushort[] SetString(int start, string value) {
            byte[] bytestemp = Encoding.Default.GetBytes(value);
            ushort[] result = Bytes2Ushorts(bytestemp);
            return result;
        }

        private static byte[] Ushorts2Bytes(ushort[] src, bool reversc = false) {
            int count = src.Length;
            byte[] dest = new byte[count << 1];
            if (reversc) {
                for (int i = 0; i < count; i++) {
                    dest[i * 2] = (byte)(src[i] >> 8);
                    dest[i * 2 + 1] = (byte)(src[i] >> 0);
                }
            } else {
                for (int i = 0; i < count; i++) {
                    dest[i * 2] = (byte)(src[i] >> 0);
                    dest[i * 2 + 1] = (byte)(src[i] >> 8);
                }
            }
            return dest;
        }
        private static ushort[] Bytes2Ushorts(byte[] src, bool reversc = false) {
            int len = src.Length;
            byte[] srcPlus = new byte[len + 1];
            src.CopyTo(srcPlus, 0);
            int count = len >> 1;
            if (len % 2 != 0) {
                count += 1;
            }
            ushort[] dest = new ushort[count];
            if (!reversc) {
                for (int i = 0; i < count; i++) {
                    dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
                }
            } else {
                for (int i = 0; i < count; i++) {
                    dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
                }
            }

            return dest;

        }
        #endregion 
    }
}
